<?php


/*   Description  of this file
*    Author king-of-war
*    Last Modified 2009-4-8
*    This class designed  for the vote operation
*/
class vote {
	private $url;
	/*
	 * init all varables
	 */
	function vote($url) {
		require_once $url . 'includes/conn.php';
		$this->set_url($url);
	}
	/*
	 * vote add function
	 *
	 *$table 1 vote   2 votes  3 vote_se
	 *
	 * fun 1 add   0 update
	 */
	function vote_add($id, $var2, $var3, $var4, $table, $fun) {
		$conn = new conn();
		if ($table == 1) {
			if ($fun) {
				$sql = sprintf("insert into vote(topic)values('%s')", $var2);
			} else {
				$sql = sprintf("update vote set topic='%s' where id='%d'", $var2, $id);
			}
		}
		elseif ($table == 2) {
			if ($fun) {
							$sql = sprintf("insert into votes(choice,vote_id,date)values('%d','%d',NOW())", $var2, $var3);
			$xml_id=$var3;
			} else {
				$sql = sprintf("update votes set choice='%d',vote_id='%d' where id='%d'", $var2, $var3, $id);
			}
		}
		elseif ($table == 3) {
			if ($fun) {
				$sql = sprintf("insert into vote_se(se_id,vote_id,contant)values('%d','%d','%s')", $var2, $var3, $var4);
			} else {
				$sql = sprintf("update vote_se set se_id='%d',vote_id='%d',contant='%s' where id='%d'", $var2, $var3, $var4, $id);
			}
		}

	 
		$conn->set_sql($sql);
		$line = $conn->get_line();
		if($line){
	   if(isset($xml_id)){
	   $this->vote_xml($xml_id);
	   }
		}
		return $line;
	}
	/*
	*   vote xml set
	*/
	function vote_xml($id){
		$url=$this->get_url();
		include_once $url.'includes/xml/xml_op.php';
		/* get title*/
		$vote=$this->vote_read($id,1);
		$title=$vote[0]->get_topic();
		/*get items */
		$items=array();
		$conn=new conn();
		$sql=sprintf("select se_id,contant from vote_se where vote_id='%d'",$id);
		$conn->set_sql($sql);
		$line=$conn->get_line();
		echo '<br/>line is:'.$line;
		if($line){
		$result=$conn->get_result();
		for($i=0;$row=$result->fetch_row();$i++){
		$items['name'][$i]=$row[1];
		$se_id=$row[0];
		$con=new conn();
		$sql=sprintf("SELECT id FROM `votes` WHERE vote_id='%d' and choice='%d'",$id,$se_id);
		$con->set_sql($sql);
		$line=$con->get_line();
		$items['date'][$i]=$line;
				}
				}
					$xml=new xml_op($root_path);
				$xml->xml_vote('includes/xml/piedata.xml',$title,$items);
		}	/*
	 * vote del function
	 */
	function vote_del($ids, $fun) {
		if (isset ($ids)) {
			/*
			 *  $fun 1 , for vote  big
			 *
			 * $fun2 for vote_se small
			 */
			if ($fun == 1) {
				$_sql = "vote ";
			}
			elseif ($fun == 2) {
				$_sql = "vote_se";
			}
			if (is_array($ids)) {
				$max = count($ids);
				for ($i = 0; $i < $max; $i++) {
					$conn = new conn();
					$sql = sprintf("delete from " . $_sql . " where id=%d ; ", $ids[$i]);
					$conn->set_sql($sql);
					$line1 = $conn->get_line();
					$line += $line1;
				}
			} else {
				$conn = new conn();
				$sql = sprintf("delete from " . $_sql . " where id=%d ", $ids);
				$conn->set_sql($sql);
				$line = $conn->get_line();
			}
		} else {
			echo 'at least choose one for process please';
			$line = 0;
		}
		return $line;
	}
	/*
	 *  truncate the table votes
	 */
	function truncate() {
		$conn = new conn();
		$sql = "truncate table votes";
		$conn->set_sql($sql);
		$line = $conn->get_line();
		return $line;
	}
	/*
	 * vote read
	 *
	 * $fun 1 as vote
	 * 2 as votes   3 as vote_se
	 */
	function vote_read($id, $fun) {
		$conn = new conn();
		$url = $this->get_url();
		if ($fun == 1) {
			/*
			 *  read  vote codes here
			 */
			include_once $url . 'includes/entity/vote.entity.php';
			$table = 'vote';
		}
		elseif ($fun == 2) {
			/*
			 *  read  vote codes here
			 */
			include_once $url . 'includes/entity/votes.entity.php';
			$table = 'votes';
		}
		elseif ($fun == 3) {
			/*
			 *  read  vote code_se here
			 */
			include_once $url . 'includes/entity/vote_se.entity.php';
			$table = 'vote_se';
		} else {
			echo 'please choose the table whith you\'ll process';
		}
		if (is_array($id)) {
			/*
			 * return the resluts as array
			 * $a means start   $b means counts
			 */
			$a = $id[0];
			$b = $id[1];
			$sql = sprintf("select * from " . $table . " limit %d,%d ", $a, $b);
		}
		elseif (!empty ($id)) {
			/*
			 *  reslut is only one
			 */
			$sql = sprintf("select * from " . $table . " where id='%d' ", $id);
		} else {
			$sql = sprintf("select * from " . $table);

		}

		$conn->set_sql($sql);
		$line = $conn->get_line();
		$votes = array ();
		if ($line) {
			$result = $conn->get_result();
			if ($fun == 1) {
				for ($i = 0; $row = $result->fetch_row(); $i++) {
					$votes[$i] = new vote_entity();
					$votes[$i]->set_id($row[0]);
					$votes[$i]->set_topic($row[1]);
				}
			}
			elseif ($fun == 2) {
				for ($i = 0; $row = $result->fetch_row(); $i++) {
					$votes[$i] = new votes_entity();
					$votes[$i]->set_id($row[0]);
					$votes[$i]->set_choice($row[1]);
					$votes[$i]->set_vote_id($row[2]);
					$votes[$i]->set_date($row[3]);
				}
			}
			elseif ($fun == 3) {
				for ($i = 0; $row = $result->fetch_row(); $i++) {
					$votes[$i] = new vote_se_entity();
					$votes[$i]->set_id($row[0]);
					$votes[$i]->set_se_id($row[1]);
					$votes[$i]->set_vote_id($row[2]);
					$votes[$i]->set_contant($row[3]);
				}
			}
		} else {
			echo 'result null';
			$votes = null;
		}
		return $votes;
	}
	/*
	 * vote count function
	 */
	function vote_count() {
	}
	/*
	 * vote display
	 *
	 * $fun=1 means vote.php   0means view php 
	 */
	function vote_display($vote_id, $fun) {
		$conn = new conn();
		$re = array ();
		if ($fun) {
			$sql = sprintf("select * from vote_se where vote_id='%d'", $vote_id);
		} else {
			$sql = sprintf("select * from votes where vote_id='%d'", $vote_id);
		}
		$conn->set_sql($sql);
		$line = $conn->get_line();
		if ($line) {
			if ($fun) {
				$result = $conn->get_result();
				for ($i = 0; $row = $result->fetch_row(); $i++) {
					$re[$i]['id'] = $row[1];
					$re[$i]['contant'] = $row[3];
				}
			}else{
				$result = $conn->get_result();
				for ($i = 0; $row = $result->fetch_row(); $i++) {
					$re[$i] = $row[1];

			}
				}
		} else {
			$re = '';
			}
		return $re;

	}
	/*
	 * common function
	 */
	function set_url($url) {
		$this->url = $url;
	}
	function get_url() {
		return $this->url;
	}
}
?>